What is lodash.mapvalues?
The lodash.mapvalues package is a utility library that provides a method to create a new object with the same keys as the original object, but with values generated by running each own enumerable string-keyed property of the object through a provided function.
What are lodash.mapvalues's main functionalities?
Transform Object Values
This feature allows you to transform the values of an object based on a function. In this example, the values of the 'users' object are transformed to just their ages.
const mapValues = require('lodash.mapvalues');
const users = { 'fred': { 'age': 40 }, 'pebbles': { 'age': 1 } };
const result = mapValues(users, function(o) { return o.age; });
console.log(result); // { 'fred': 40, 'pebbles': 1 }
Modify Values Based on Key
This feature allows you to modify the values of an object based on both the value and the key. In this example, the values are transformed to a string that includes both the key and the age.
const mapValues = require('lodash.mapvalues');
const users = { 'fred': { 'age': 40 }, 'pebbles': { 'age': 1 } };
const result = mapValues(users, function(o, key) { return key + ' is ' + o.age; });
console.log(result); // { 'fred': 'fred is 40', 'pebbles': 'pebbles is 1' }
Other packages similar to lodash.mapvalues
ramda
Ramda is a functional programming library for JavaScript that provides a similar function called 'R.map'. It allows you to map over the values of an object, but it also offers a broader range of functional programming utilities compared to lodash.mapvalues.
underscore
Underscore is another utility library that provides a similar function called '_.mapObject'. It offers a wide range of utility functions for JavaScript, similar to Lodash, but with a different API and some differences in performance and functionality.
lodash.mapvalues v4.6.0
The lodash method _.mapValues
exported as a Node.js module.
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.mapvalues
In Node.js:
var mapValues = require('lodash.mapvalues');
See the documentation or package source for more details.